package com.infitecs.eyas.mill.service;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.mill.dao.PipeCalculateDao;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.modal.StepInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.Arc2D;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/28 0028.
 */
@Service
public class PipeCalculateService {

    @Autowired
    private PipeCalculateDao pipeCalculateDao;

    @Autowired
    private SessionService sessionService;
    @Autowired
    private MillSelectService millSelectService;
    @Autowired
    private ExpressionService expressionService;

    public PipeCalculate getPipeCalculate() {
        return pipeCalculateDao.getPipeCalculate(sessionService.getCaseId());
    }

    public void addPipeCalculate() {
        int caseId = sessionService.getCaseId();
        pipeCalculateDao.addPipeCalculate(caseId);
        sessionService.addStepState(new StepInfo(caseId, Constant.STEP_ID_14, Constant.STEP_OVER_STATE_0));
    }

    public void saveValueOfPipeCalculate(Param param) {
        param.setCaseId(sessionService.getCaseId());
        pipeCalculateDao.saveValueOfPipeCalculate(param);
        param.setParamName(sessionService.getE() + param.getParamName());
        sessionService.putParamMap(param);
    }

    public void pipelineSave(PipeCalculate pipeCalculate) {
        pipelineDataSave(pipeCalculate);
    }

    public void pipelineDataSave(PipeCalculate pipeCalculate) {
        Map<String, Formula> formulaMap = expressionService.getFormulaMapByType(7);
        Map<String, Double> paramMap = millSelectService.getChooseDataByCaseId(pipeCalculate.getCaseId());
        paramMap.put("E115", pipeCalculate.getE115());
        double E116 = expressionService.getCalculateData(formulaMap.get("AE116"), paramMap);
        paramMap.put("E116", E116);
        double E117 = expressionService.getCalculateData(formulaMap.get("AE117"), paramMap);
        paramMap.put("E117", E117);
        paramMap.put("E118", pipeCalculate.getE118());
        paramMap.put("E119", pipeCalculate.getE119());
        paramMap.put("EPI314", Math.PI);
        double AE120_1 = expressionService.getCalculateData(formulaMap.get("AE120_1"), paramMap);
        double AE120_2 = expressionService.getCalculateData(formulaMap.get("AE120_2"), paramMap);
        if (22 <= AE120_1 && AE120_2 <= 28) {
            double AE120_3 = expressionService.getCalculateData(formulaMap.get("AE120_3"), paramMap);
            paramMap.put("AE120", AE120_3);
            pipeCalculate.setE120(AE120_3);
        } else {
            pipeCalculate.setE120(null);
        }
        pipeCalculate.setE116(E116);
        pipeCalculate.setE117(E117);
        PipeCalculate oldData = pipeCalculateDao.getPipeCalculate(pipeCalculate.getCaseId());
        if (oldData == null) {
            pipeCalculateDao.addPipeCalculateData(pipeCalculate);
        } else {
            pipeCalculateDao.updatePipeCalculateData(pipeCalculate);
        }
    }
}
